package service.impl;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.ws.rs.core.MediaType;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ita.iosa.util.JsonUtil;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.core.util.MultivaluedMapImpl;

import service.WebService;
import vo.Dish;
import vo.Merchant;
import vo.Orders;

@Service("webService")
public class WebServiceImpl implements WebService {

	private static ObjectMapper mapper = new ObjectMapper();
	private Client client;
	private static Properties properties=new Properties();

	public WebServiceImpl() {
		client = Client.create();
		client.setReadTimeout(5000);
	}

	@Override
	public Map<String,Orders> listAllOrders(List<String> oIdList) {
		try {
			properties.load(new FileInputStream(WebServiceImpl.class.getClassLoader().getResource("webService.properties").getPath()));
		} catch (IOException e2) {
			System.out.println("properties is not found!");
			e2.printStackTrace();
		}
		String url = properties.getProperty("getOrders_url");
		WebResource wr = client.resource(url);
		MultivaluedMapImpl params = new MultivaluedMapImpl();  
	    try {
			params.add("oids", JsonUtil.objetc2Json(oIdList));
		} catch (JsonProcessingException e1) {
			e1.printStackTrace();
		}  
		String jsonData = wr.accept(MediaType.APPLICATION_JSON_TYPE).post(String.class,params);
		Map<String,Orders> map = null;
		try {
			map = mapper.readValue(jsonData, new TypeReference<Map<String, Orders>>(){});
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return map;
	}

	@Override
	public Merchant findMerchantByMId(String mId) {
		try {
			properties.load(new FileInputStream(WebServiceImpl.class.getClassLoader().getResource("webService.properties").getPath()));
		} catch (IOException e2) {
			System.out.println("properties is not found!");
			e2.printStackTrace();
		}
		String url = properties.getProperty("queruMerchant_url");
		WebResource wr = client.resource(url+mId);
		String jsonData = wr.accept(MediaType.APPLICATION_JSON_TYPE).get(String.class);
		Merchant merchantVo = null;
		try {
			merchantVo = mapper.readValue(jsonData, new TypeReference<Merchant>(){});
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return merchantVo;
	}

	@Override
	public Map<String, Merchant> listAllMerchant() {
		try {
			properties.load(new FileInputStream(WebServiceImpl.class.getClassLoader().getResource("webService.properties").getPath()));
		} catch (IOException e2) {
			System.out.println("properties is not found!");
			e2.printStackTrace();
		}
		String url = properties.getProperty("queryAllMerchants_url");
		WebResource wr = client.resource(url);
		String jsonData = wr.accept(MediaType.APPLICATION_JSON_TYPE).get(String.class);
		Map<String, Merchant> map = null;
		try {
			map = mapper.readValue(jsonData, new TypeReference<Map<String, Merchant>>(){});
		} catch (IOException e) {
			e.printStackTrace();
		}
		return map;
	}

	@Override
	public Map<String, Dish> listDishByDId(List<String> dIdList) {
		try {
			properties.load(new FileInputStream(WebServiceImpl.class.getClassLoader().getResource("webService.properties").getPath()));
		} catch (IOException e2) {
			System.out.println("properties is not found!");
			e2.printStackTrace();
		}
		String url = properties.getProperty("queryDish_url");
		WebResource wr = client.resource(url);
		MultivaluedMapImpl params = new MultivaluedMapImpl();  
		try {
			params.add("dids", JsonUtil.objetc2Json(dIdList));
		} catch (JsonProcessingException e1) {
			e1.printStackTrace();
		}
		String jsonData = wr.accept(MediaType.APPLICATION_JSON_TYPE).post(String.class,params);
		Map<String, Dish> map = null;
		try {
			map = mapper.readValue(jsonData, new TypeReference<Map<String, Dish>>(){});
		} catch (IOException e) {
			e.printStackTrace();
		}
		return map;
	}

	@Override
	public Map<String, Merchant> listMerchantByMId(List<String> mIdList) {
		try {
			properties.load(new FileInputStream(WebServiceImpl.class.getClassLoader().getResource("webService.properties").getPath()));
		} catch (IOException e2) {
			System.out.println("properties is not found!");
			e2.printStackTrace();
		}
		String url = properties.getProperty("queryMerchants_url");
		WebResource wr = client.resource(url);
		MultivaluedMapImpl params = new MultivaluedMapImpl();  
		try {
			params.add("mids", JsonUtil.objetc2Json(mIdList));
		} catch (JsonProcessingException e1) {
			e1.printStackTrace();
		}  
		String jsonData = wr.accept(MediaType.APPLICATION_JSON_TYPE).post(String.class,params);
		Map<String, Merchant> map = null;
		try {
			map = mapper.readValue(jsonData, new TypeReference<Map<String, Merchant>>(){});
		} catch (IOException e) {
			e.printStackTrace();
		}
		
//		Map<String, Merchant> map = null;
		return map;
	}

}
